﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class new_add : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            string newsTypeId = "";
            if (Request["newsTypeId"] != null && Request["newsTypeId"] != "null" && !string.IsNullOrEmpty(Request["newsTypeId"]))
            {
                newsTypeId = Request["newsTypeId"].ToString();
                lblSelectType.Text = dal.NewsType.First(x => x.id == newsTypeId).name;
            }
            DataList1.DataSource = dal.NewsType.Where(x => string.IsNullOrEmpty(x.pid));
            DataList1.DataBind();
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string newsTypeId = Request["newsTypeId"].ToString();
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.News news = new CMSModel.News();
            news.author = txtAuthor.Text;
            news.ctime = DateTime.Now;
            news.content = txtContent.Text;
            news.keyword = txtKeyWords.Text;
            news.title = txtTitle.Text;
            news.Summary = txtSummary.Text;
            news.Stock = txtStock.Text;
            news.Source = txtSource.Text;
            news.step = int.Parse(ddlStep.SelectedValue);
            news.OutsideUrl = txtOutsideUrl.Text;
            if (Request["aa"] == "1")
            {
                news.IsEnable = true;
                news.ptime = news.ctime;
            }
            else
            {
                news.IsEnable = false;
            }
            news.News_Type.Add(new CMSModel.News_Type() { ctime = DateTime.Now, News = news, NewsTypeId = newsTypeId });
            dal.News.AddObject(news);
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='default.aspx?newsTypeId=" + newsTypeId + "'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "AddNews", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('添加失败,请重试');", true);
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var c = (NewsType_NewsTypeSelect)e.Item.FindControl("NewsTypeSelect1");
        c.BindData(0);
    }
}